package cn.jgls.bi.dataprocess;

import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Iterator;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import cn.jgls.bi.dataprocess.ArchiDept.*;
import java.util.Calendar;

/**
 *
 * @author zz
 */
public class MainClass {

    public static void main(String[] args) throws Exception {
        String ls_fname;
        String ls_input_date = "";
        String ls_DateNo = "";
        Date ld_date = null;
        java.sql.Date ld_sqldate = null;
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dd2 = new SimpleDateFormat("E");
        SimpleDateFormat dd3 = new SimpleDateFormat("yyyy");
        int shopId = 0;
        int i = 0;
        int yybh;
        int man_id;
        int y, m, d;
        int li_dateNo;
        int liBj = 0;
        String shopBm;
        String ls_Year;
        ArrayList<String> ls_name = new ArrayList<String>();
        ArrayList<ArchiDept> lArchiD = new ArrayList<ArchiDept>();
        Calendar cal = Calendar.getInstance();
        if (args.length > 0) {
            ls_input_date = args[0];
            try {
                ld_date = dd.parse(ls_input_date);
            } catch (ParseException e) {
                System.out.println("Please input the Date like this format yyyy-mm-dd eg:2007-12-01");
            }
            y = Integer.parseInt(ls_input_date.substring(0, 4));
            m = Integer.parseInt(ls_input_date.substring(5, 7));
            m = m - 1;
            d = Integer.parseInt(ls_input_date.substring(8));
            cal.set(y, m, d);
        } else {
            cal.setTimeInMillis(System.currentTimeMillis());
            cal.add(Calendar.DATE, -1);
        }
        
        ls_input_date = dd.format(cal.getTime());
        li_dateNo = cal.get(Calendar.DAY_OF_WEEK);
        ls_Year = dd3.format(cal.getTime());
        ld_sqldate = java.sql.Date.valueOf(ls_input_date);


        //判断数据源
        String sTname = "";
        java.util.Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.DATE, -1);
        String lsYesterday = dd.format(rightNow.getTime() );
        java.sql.Date ldYesterday = java.sql.Date.valueOf(lsYesterday);

        sTname = " t_waste_book1 ";


        ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig_Jdbc.xml");
        DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
        DataProcessJdbcDao taskDao = new DataProcessJdbcDao();
        taskDao.setDataSource(ds); //交易笔数

//        Iterator fctIter = taskDao.getFunction().iterator();
//        i = 0;
//        while (fctIter.hasNext()) {
//            ls_name.add(fctIter.next().toString().trim());
//        }
        Iterator ShopIter = taskDao.getShopList(ld_sqldate, liBj).iterator();
        ArchiDept ad;
        while (ShopIter.hasNext()) {
            ad = (ArchiDept) ShopIter.next();
            lArchiD.add(ad);
        }

        //并发执行

//        for (i = 0; i < ls_name.size(); i++) {

            //ls_fname = ls_name.get(i).toString();
            ls_fname = "batchupdate";
            ThreadWorker t1 = new ThreadWorker(lArchiD);
            t1.setParm(ld_sqldate, li_dateNo, ls_Year, sTname, ls_fname);
            t1.start();
//        }
    }
}
